import Vue from "vue";

// 节流:使用此方法需要在data中声明 onoff:true
let onoff = true
Vue.prototype.$throttle = function throttle(fn, wait = 200) {
  let that = this;
  if (that.$onoff) {
    that.$onoff = false;
    fn();
    setTimeout(() => {
      that.$onoff = true;
    }, wait)
  } else {
    console.log("稍后再试");
  }
}
Vue.prototype.$debounce = function debounce(fn, wait) {
  let delay = wait || 500
  let timer
  return function () {
    let args = arguments;
    if (timer) {
      clearTimeout(timer)
      console.log('拦截')
    }
    let callNow = !timer
    timer = setTimeout(() => {
      console.log('发送')
      timer = null
    }, delay)
    if (callNow) fn.apply(this, args)
  }
}